package com.ict.txmsv2.services;

import cn.hutool.core.collection.CollectionUtil;
import com.ict.txmsv2.mapper.ClockInMapper;
import com.ict.txmsv2.mapper.UserMapper;
import com.ict.txmsv2.model.ClockIn;
import com.ict.txmsv2.model.RespPageBean;
import com.ict.txmsv2.model.User;
import com.ict.txmsv2.utils.SdfBuilder;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yoleen
 * @date 2020/12/17 12:15
 */

@Service
public class ClockInService {
    @Autowired
    ClockInMapper clockInMapper;
    @Autowired
    UserMapper userMapper;

    public int count(int userId){
        return clockInMapper.count(userId);
    }

    public int insert(ClockIn clockIn) {
        return clockInMapper.newInsert(clockIn);
    }

    public List<ClockIn> getAllClockIns() {
        return clockInMapper.getAllClockIns();
    }

    public List<List<List<String>>> getCommute() {
        Calendar c = Calendar.getInstance();
        int pre = c.get(Calendar.DAY_OF_WEEK);
        pre -= 2;
        c.add(Calendar.DAY_OF_YEAR,-pre);
        String begin = SdfBuilder.getSimpleDateFormat().format(c.getTime());
        c.add(Calendar.DAY_OF_YEAR,6);
        String end = SdfBuilder.getSimpleDateFormat().format(c.getTime());
        List<ClockIn> lists = clockInMapper.getAllClockInsForWeek(begin, end);
        List<List<List<String>>> ans = new ArrayList<>();
        ans.add(new ArrayList<>());
        ans.add(new ArrayList<>());
        HashMap<Integer,Integer> map = new HashMap<>();
        for (ClockIn clockIn : lists) {
            List<String> data = new ArrayList<>();
            data.add(clockIn.getDt());
            data.add(clockIn.getDtt());
            data.add(clockIn.getName());
            int orDefault = map.getOrDefault(clockIn.getUserid(), 0);
            if(orDefault%2==0){
                data.add("上班打卡");
                ans.get(0).add(data);
            }else {
                data.add("下班打卡");
                ans.get(1).add(data);
            }
            map.put(clockIn.getUserid(),orDefault+1);
        }
        return ans;
    }

    public RespPageBean getCommuteByDayWithPage(String day, Integer page,Integer size){
        RespPageBean res = new RespPageBean();
        if(page!=null && page > 0){
            page = (page - 1)*size;
        }else {
            return res;
        }
        long total = userMapper.getClockInTotal(day);
        List<Integer> ids = userMapper.getIdsWithPage(day,page,size);
        List<User> usersWithClockInByDay = userMapper.getUsersWithClockInByDayWithPage(day,ids);
        List<List<String>> ans = new ArrayList<> ();
        List<String> temp = null;
        for (User user : usersWithClockInByDay) {
            List<ClockIn> clockIn = user.getClockIn();
            temp = new ArrayList<> ();
            if (CollectionUtil.isNotEmpty(clockIn)) {
                for (int i = 1; i <= clockIn.size(); i++) {
                    ClockIn c = clockIn.get(i - 1);
                    if (i%2==0){
                        temp.add(c.getDt()+" "+c.getDtt());
                        ans.add(temp);
                        temp=new ArrayList<> ();
                    }else {
                        temp.add(user.getName());
                        temp.add(c.getDt()+" "+c.getDtt());
                        if (i==clockIn.size()){
                            ans.add(temp);
                        }
                    }
                }
            }
        }
        res.setData(ans);
        res.setTotal(total);
        return res;
    }
}
